const app = getApp();
const pageSize = 20;
const statusList = [{
  value: "",
  name: '全部'
}, {
  value: 1,
  name: '审核中'
}, {
  value: 2,
  name: '审核通过'
}, {
  value: 3,
  name: '审核拒绝'
}];

Page({
  data: {
    applyList: [],
    nomore: false,
    loadmore: false,
    page: 1, //当前页数
    conditions: {
      card: '',
      start_time: '',
      end_time: '',
      tel: '',
      statusIndex: "",
      typeIndex: "",
      areaInfo_name: [],
      area_id_Info: [],
    },
    statusList,
    typeList: [],
    empty: false,
    drawer: false,
    isFilter: false,
  },


  onLoad: function (options) {
    this.getApplyList();
    this.getCarType();
  },

  onReachBottom: function () {
    if (this.data.loadmore) {
      this.setData({
        page: this.data.page + 1
      })
      this.getApplyList();
    }
  },

  onPullDownRefresh: function () {
    this.reloadData();
  },


  reloadData() {
    this.setData({
      page: 1,
      applyList: [],
      loadmore: false,
      nomore: false
    })
    this.getApplyList();
  },

  onReady: function () {
    this.cascade = this.selectComponent("#cascade");
  },

  //打开联动地址组件
  showCascade() {
    this.cascade.selection(this.data.area_id_Info);
  },

  //联动地址组件complete
  cascadeComplete(e) {
    const detail = e.detail;
    this.setData({
      "conditions.areaInfo_name": detail.names,
      "conditions.area_id_Info": detail.ids,
    })
  },






  // 查看/修改
  operation: function (e) {
    let applyIndex = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: '../../specialApply/specialApply?id=' + this.data.applyList[applyIndex].id + '&type=' + e.currentTarget.dataset.type + "&from=2"
    })
  },




  //获取申请列表
  getApplyList() {
    app.ajax({
      url: 'Partner/WaterCard/waterCardAuditList',
      data: {
        page: this.data.page,
        row: pageSize,
        card: this.data.conditions.card,
        card_type: this.data.conditions.typeIndex !== "" ? this.data.typeList[this.data.conditions.typeIndex].id : "",
        card_class: 2,
        start_time: this.data.conditions.start_time,
        end_time: this.data.conditions.end_time,
        tel: this.data.conditions.tel,
        province: this.data.conditions.area_id_Info[0],
        city: this.data.conditions.area_id_Info[1],
        area: this.data.conditions.area_id_Info[2],
        audit_status: this.data.conditions.statusIndex !== "" ? this.data.statusList[this.data.conditions.statusIndex].value : "",
        type: 1,
        is_special:1,
      },
      success: res => {
        wx.hideLoading();
        wx.stopPullDownRefresh()
        if (res.data.code == 1000) {
          const source = res.data.data;
          let list = this.data.applyList.concat(source.data);
          this.setData({
            applyList: list,
            nomore: list.length >= source.page.total_number ? true : false,
            loadmore: list.length < source.page.total_number ? true : false,
            empty: source.page.total_number == 0 ? true : false,
            drawer: false
          })
        } else {
          app.showToast(res.data.message);
        }
      },
      error: e => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        app.showToast("网络错误");
      }
    })
  },




  //获取卡类型
  getCarType() {
    var that = this;
    app.ajax({
      url: 'Partner/WaterCard/getCarType',
      method: "POST",
      data: {
        type: 2,
        is_apply: 1,
        is_search: 1
      },
      success: function (res) {
        var flag = res.data;
        if (flag.code == 1000) {
          that.setData({
            typeList: flag.data
          });

        }
      }
    });
  },



  handleTime(e) {
    const name = e.currentTarget.dataset.name;
    this.setData({
      [`conditions.${name}`]: e.detail.value
    })
  },


  handleChange(e) {
    const {
      index,
      name
    } = e.currentTarget.dataset;
    this.setData({
      [`conditions.${name}`]: index
    })
  },



  showDrawer() {
    this.condition = {
      ...this.data.conditions
    };
    this.setData({
      drawer: true,
    })
  },

  closeDrawer() {
    this.setData({
      drawer: false,
      conditions: this.condition
    })
    delete(this.condition)
  },

  resetDrawer() {
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] = "";
    }
    this.setData({
      conditions,
    })
  },

  confirmDrawer() {
    let isFilter = false;
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] !== "" && (isFilter = true);
    }
    this.setData({
      isFilter
    })
    delete(this.condition);
    this.reloadData();
  },



})